package com.pyip.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pyip.reggie.Entity.Category;
import com.pyip.reggie.Entity.Setmeal;
import com.pyip.reggie.Entity.SetmealDish;
import com.pyip.reggie.common.CustomException;
import com.pyip.reggie.common.R;
import com.pyip.reggie.dto.SetmealDto;
import com.pyip.reggie.mapper.SetmealMapper;
import com.pyip.reggie.service.CategoryService;
import com.pyip.reggie.service.SetmealDishService;
import com.pyip.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private CategoryService categoryService;

    @Transactional
    @Override
    public R<String> saveSetMealWithSetMealDish(SetmealDto setmealDto) {
        // 添加套餐信息 首先添加套餐，再在套餐dish中添加菜品
        baseMapper.insert(setmealDto);

        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes.stream().map((item) -> {
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());
        // 添加setmealDish表中的数据
        ;

        return setmealDishService.saveBatch(setmealDishes) ? R.success("添加套餐成功") : R.error("添加失败");
    }

    @Override
    public R<Page<SetmealDto>> getPage(Long page, Long pageSize, String name) {
        Page<Setmeal> setmealPage = new Page<>(page, pageSize);
        Page<SetmealDto> setmealDtoPage = new Page<SetmealDto>();

        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.like(name != null,Setmeal::getName,name);
        baseMapper.selectPage(setmealPage,setmealLambdaQueryWrapper);

        BeanUtils.copyProperties(setmealPage,setmealDtoPage,"records");
        List<Setmeal> records = setmealPage.getRecords();
        List<SetmealDto> dtoRecords = records.stream().map((item) -> {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(item, setmealDto);
            Category category = categoryService.getById(item.getCategoryId());
            String categoryName = category.getName();
            setmealDto.setCategoryName(categoryName);
            return setmealDto;
        }).collect(Collectors.toList());

        setmealDtoPage.setRecords(dtoRecords);
        return R.success(setmealDtoPage);
    }

    @Transactional
    @Override
    public R<String> deleteSetMealAndDishByIds(List<Long> ids) {
        LambdaQueryWrapper<Setmeal> slqw = new LambdaQueryWrapper<>();
        slqw.in(Setmeal::getId,ids);
        slqw.eq(Setmeal::getStatus,1);
        if(baseMapper.selectCount(slqw) > 0){
            throw new CustomException("此商品正在售卖，请停售后再删除");
        }
        baseMapper.deleteBatchIds(ids);

        // 删除关联表中的信息
        LambdaQueryWrapper<SetmealDish> sdlqw = new LambdaQueryWrapper<>();
        sdlqw.in(SetmealDish::getSetmealId,ids);
        return setmealDishService.remove(sdlqw) ? R.success("删除成功") : R.error("删除失败");
    }

    @Override
    public R<List<Setmeal>> getList(Setmeal setmeal) {
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(setmeal.getCategoryId() !=
                null,Setmeal::getCategoryId,setmeal.getCategoryId());
        queryWrapper.eq(setmeal.getStatus() !=
                null,Setmeal::getStatus,setmeal.getStatus());
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);

        return R.success(this.list(queryWrapper));
    }
}
